

import { createSlice,createAsyncThunk,PayloadAction } from '@reduxjs/toolkit'

// 路由的state
export interface routerState{
    name:string,
    router:string,
    key:string,
    // routerData:Array<shoodata.routerSataData>,
    meta?:{
        [propName: string]: string|number
    },
}


 export interface sert{
    routerData1:Array<routerState>,
    contoent:number
}
const initialState:sert={
    routerData1:[],
    contoent:0
}

// 异步函数
// export const incrementAsync = createAsyncThunk(
//     "router/Async",
//     async (data:routerState)=>{
//         console.log("获取传过来的数据---",data)
//     const res = await new Promise<routerState>(r=>{
//       setTimeout(() => {
//         r(data)
//       }, 20);
//     })
//     return res
//   })

export const counterRouter=createSlice({
    name:"router",
    initialState,
    // 同步函数
    reducers:{
        updated :(state:sert,newData:any) =>{
            
            // 判断是否存在，如果已经有了就不重复添加了
            if(state.routerData1.length>0){

                var newData15:any=state.routerData1.find((item:any)=>{
                   
                    return item.payload.key==newData.payload.key
                })
                if(!newData15){
                    state.routerData1.push(newData)
                }
                
                
            }else{
                state.routerData1.push(newData)
            }
             
             
        },
        addData:(stat:sert)=>{
            stat.contoent++
        }
    },
    // extraReducers(builder){
        // 这里的payload就是incrementAsync的返回值
        // builder.addCase(incrementAsync.fulfilled,(state:sert,newData:any)=>{
           
            // state.label=newData.payload.label
            // state.path=newData.payload.path
            // if(!newData.payload.meta){
            //     state.meta=newData.payload.meta
            // }
           
    //     })
    // }
})
// 这里的actions对应的是上面的reducers function
export const { updated,addData } = counterRouter.actions

export default counterRouter.reducer